import requests
import json


def api_call(api_key, city):
    base_url = "http://api.openweathermap.org/data/2.5/weather"
    params = {
        'q': city,
        'appid': api_key,
        'units': 'metric'
    }
    response = requests.get(base_url, params=params)
    weather_data = response.json()
    return weather_data


# 获取天气
def translate_weather(weather_data):
    weather_dict = {
        "Clear": "晴朗",
        "Clouds": "多云",
        "Rain": "雨",
        "Snow": "雪",
        # 添加更多天气翻译
    }
    weather_description = weather_data['weather'][0]['main']
    weather_description_cn = weather_dict.get(weather_description, "未知")  # 根据返回的value为键,在获取对应的翻译
    return weather_description_cn

weather_data = api_call(api_key='531b7f1951343b16990631805421a41e',city='Shanghai')

print(weather_data)
print(json.dumps(weather_data, indent=4))
